package com.limao.DBProvider;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.limao.DBDataClass.CrowdSysPriceDataClass;
import com.limao.DbConnection.DbConnectionManager;

public class CrowdSysPriceProvider 
{
	private static CrowdSysPriceProvider instance;

	public static CrowdSysPriceProvider getInstance() {
		if (instance == null)
			instance = new CrowdSysPriceProvider();
		return instance;
	}

	private CrowdSysPriceProvider() {

	}

	private CrowdSysPriceDataClass encapOrderData(ResultSet rs) throws Exception {

		CrowdSysPriceDataClass orderData = new CrowdSysPriceDataClass();
		orderData.priceid = rs.getString("priceid");
		orderData.minprice = rs.getString("minprice");
		orderData.maxprice = rs.getString("maxprice");
		orderData.about = rs.getString("about");
		orderData.sortvalue = rs.getString("sortvalue");
		return orderData;
	}
	
	
	public CrowdSysPriceDataClass load_PriceByPriceid(int priceid) {
		CrowdSysPriceDataClass tmp = null;
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {

			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement("select * from cd_sys_price where priceid=? ");
			pstmt.setInt(1, priceid);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				tmp = encapOrderData(rs);
			}

		} catch (Exception e) {
			e.printStackTrace();
			tmp = null;
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}
		return tmp;
	}
	
	public List<CrowdSysPriceDataClass> load_PriceList() {
		
		List<CrowdSysPriceDataClass> list=new ArrayList<CrowdSysPriceDataClass>();
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {

			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement("select * from cd_sys_price where state=1 order by sortvalue asc");
			rs = pstmt.executeQuery();
			while (rs.next()) {
				list.add(encapOrderData(rs));
			}

		} catch (Exception e) {
			e.printStackTrace();
			list = null;
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}
		return list;
	}	
	
}
